//
//  CategoryViewController.m
//  LimitFree
//
//  Created by LZXuan on 15-6-9.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "CategoryViewController.h"
#import "AFNetworking.h"
#import "CategoryModel.h"
#import "UIImageView+WebCache.h"



@interface CategoryViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArr;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self creatAf];
}
- (void)creatTableView {
    
    self.dataArr = [[NSMutableArray alloc] init];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    [self.view addSubview:self.tableView];
}
#pragma mark - 下载数据
- (void)creatAf {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    __weak typeof(self)weakSelf = self;
    
    [manager GET:kCateUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"分类下载完成");
        if (responseObject) {
            //json 最外层是一个数组
            NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in arr) {
                CategoryModel *model = [[CategoryModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [weakSelf.dataArr addObject:model];
            }
            //刷新
            [weakSelf.tableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"分类下载失败");
    }];
}
#pragma mark - tableView协议
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    //用带主副标题的
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    CategoryModel *model = self.dataArr[indexPath.row];
    //圆角
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 5;
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed: @"account_candou"]];
    cell.textLabel.text = model.categoryCname;
    
    if ([self.categrory isEqualToString:kFreeType]) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"总共应用%@款,其中 免费中 %@款",model.categoryCount,model.free];
    }else {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"总共应用%@款,其中 限免中 %@款",model.categoryCount,model.limited];
        
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 57;
}
#pragma mark - 选中cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //把分类id 传给上级界面，上级界面要根据 id 重新下载新数据
    //我们采用block 回调的形式 进行传值
    CategoryModel *model = _dataArr[indexPath.row];
    //调用block
    if (_myBlock) {
        _myBlock(model.categoryId);
    }
    //返回上级界面
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setMyBlock:(CategoryBlock)myblock {
    if (_myBlock != myblock) {
        _myBlock = [myblock copy];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
